package com.hongxun.design.jungong.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.CreateProjectActivity;
import com.hongxun.design.adapters.MultiSelectAdapter;
import com.hongxun.design.bean.UserBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.fragments.BaseFragment;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.ShardPreferencesTool;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.object_properties.MyMessage;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cqcqwucp on 2017/9/18.单位同事
 */

public class FragmentColleagues extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    private View view;
    private SwipeRefreshListView smlistview;
    private HeadSecondControlPanel headsecond_layout;

    private MultiSelectAdapter multiAdapter;//多选适配器
    private String stage_id = "0", DataCode = "11", TYPENUM = "3", id;
    private List<UserBean> unitUsersData;//人员数据

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            smlistview.setRefreshing(false);
            switch (msg.what) {
                case HttpStringUtil.SUCCESSED:
                    if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                        unitUsersData = null;
                        unitUsersData = FastJsonUtil.JsonPersonnelData((String) msg.obj, DataCode).get(0).getUnitsUser();
                        if (unitUsersData == null) return;
                        multiAdapter = new MultiSelectAdapter(unitUsersData, getActivity(), id);
                        smlistview.setAdapter(multiAdapter);
                        smlistview.update();
                    }
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.colleagues, container, false);
        findViewById();
        return view;
    }

    @SuppressLint("NewApi")
    private void findViewById() {
        stage_id = getActivity().getIntent().getExtras().getString(MString.stage_id, "");
        DataCode = getActivity().getIntent().getExtras().getString(MString.SENDCODE);
        TYPENUM = getActivity().getIntent().getExtras().getString(MString.TYPENUM);
        id = getActivity().getIntent().getExtras().getString(MString.userid, "");

        headsecond_layout = (HeadSecondControlPanel) view.findViewById(R.id.headsecond_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean(MString.Save, View.VISIBLE, R.color.bar_right,
                ListenerSet.back, "单位同事", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
        headsecond_layout.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<UserBean> back = (List<UserBean>) multiAdapter.getChecked();
                if (back.size() > 0) {
                    CreateProjectActivity.setValue(Constant.ACTIVITYMULTISELECT, multiAdapter.getChecked(), TYPENUM);
                    getActivity().finish();
                } else {
                    ToastShow.showShort(getActivity(), "选择不能为空！");
                }
            }
        });
        unitUsersData = new ArrayList<>();
        smlistview = (SwipeRefreshListView) view.findViewById(R.id.material_srlview_coll);
        smlistview.setOnRefreshListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

    @Override
    public void onRefresh() {
        smlistview.setRefreshing(true);
        if (MyApp.isOnLine) {
            MyApp.HTTP.getPersonnelList(handler, HttpStringUtil.SUCCESSED, DataCode, stage_id);
        } else {
            handler.sendMessage(MyMessage.getMessage(HttpStringUtil.SUCCESSED,
                    ShardPreferencesTool.getshare(getActivity(), MString.UserByTypeList + DataCode, "")));
        }
    }
}
